#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.jmx;

import lombok.extern.slf4j.Slf4j;

import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;


/**
 * Description: 无法补充
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
public class JmxTool {

    /**
     * Description:
     * <连接远程JVM主机>
     * @author wupanhua
     * @date 15:54 2019/8/6
     * @param ip 1
     * @param port 2
     * @param name 3
     * @param password 4
     * @return javax.management.remote.JMXConnector
     **/
    public static JMXConnector connectRemoteJvm(String ip, String port, String name, String password) {

        JMXConnector jmxConnector;
        try {
            // TODO 这里判断一下name和password
            JMXServiceURL jmxServiceUri = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + ip + ":" + port + "/jmxrmi");
            jmxConnector = JMXConnectorFactory.connect(jmxServiceUri);
        } catch (IOException e) {
            log.error("无法创建JMX远程连接,请检查传入IP和端口号", e);
            return null;
        }

        return jmxConnector;
    }


    /**
     * Description:
     * <关闭一个远程连接>
     * @author wupanhua
     * @date 15:55 2019/8/6
     * @param jmxConnector 1
     **/
    public static void destoryRemoteConnect(JMXConnector jmxConnector) {
        try {
            if (jmxConnector != null) {
                jmxConnector.close();
            }
        } catch (IOException e) {
            log.error("无法创建JMX远程连接,请检查传入IP和端口号", e);
        }
    }
}
